package com.find.answer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.find.answer.domain.common.BaseContextHolder;
import com.find.answer.domain.http.Result;
import com.find.answer.domain.po.ShoppingCart;
import com.find.answer.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Seon
 * @date 2022/11/2 0:21
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContextHolder.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 重复代码提取，根据userId，dishId和setmealId来获取对应的处理后wrapper。
     * @param shoppingCart
     * @return
     */
    private LambdaQueryWrapper<ShoppingCart> getShoppingCartWrapper(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContextHolder.getCurrentId());
        wrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        return wrapper;
    }

    @PostMapping("/add")
    public Result<?> addGoods(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = getShoppingCartWrapper(shoppingCart);
        ShoppingCart cartOne = shoppingCartService.getOne(wrapper);
        if (cartOne == null) {
            shoppingCart.setUserId(BaseContextHolder.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartOne = shoppingCart;
        } else {
            Integer number = cartOne.getNumber();
            cartOne.setNumber(number + 1);
            shoppingCartService.updateById(cartOne);
        }
        /*
        在找到对应的购物车消息后，就把他对应的数量加一。
         */
        return Result.success(cartOne);
    }

    @PostMapping("/sub")
    public Result<?> subGoods(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = getShoppingCartWrapper(shoppingCart);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        if (number > 0) {
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
            return Result.success(one);
        } else {
            return Result.error("数量再减就成负数了");
        }
    }

    @DeleteMapping("/clean")
    public Result<?> cleanGoods() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContextHolder.getCurrentId());
        shoppingCartService.remove(wrapper);
        return Result.success("已清空购物车");
    }

}
